package edu.aut.bme.hu.orkomet;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.PopupMenu;

import com.soyatec.eface.core.collections.ObservableCollection;
import com.soyatec.eface.upf.Button;
import com.soyatec.eface.upf.ListBox;
import com.soyatec.eface.upf.StackPanel;
import com.soyatec.eface.upf.Visibility;
import com.soyatec.eface.upf.Window;
import com.soyatec.eface.upf.actions.RoutedEventArgs;
import com.soyatec.eface.upf.metadata.BooleanProperty;
import com.soyatec.eface.upf.metadata.DependencyProperty;

public class Main extends Window {
	private MenuItem mniStart, mniStop, mniExit;
	protected Button btnStart;
	protected Button btnStop;
	protected Button btnExit;
	protected StackPanel stpControlPanel;
	protected ListBox stpLog;
	public static DependencyProperty StartEnabledProperty= new BooleanProperty("StartEnabled",true); //$NON-NLS-1$
	public static DependencyProperty StopEnabledProperty= new BooleanProperty("StopEnabled",false); //$NON-NLS-1$
	protected Window mainWindow;

	public boolean getStartEnabled() {
		return Boolean.valueOf(getValue(StartEnabledProperty).toString());
	}

	public boolean getStopEnabled() {
		return Boolean.valueOf(getValue(StopEnabledProperty).toString());
	}
	

	public ObservableCollection<LogRecordView> getLogRecords() {
		return Application.getCurrentInstance().getLogRecords();
	}

	private void initTray() {
		if (SystemTray.isSupported()) {
			// get the SystemTray instance
			SystemTray tray = SystemTray.getSystemTray();
			if (tray.getTrayIcons().length > 0)
				return;
			// load an image
			Image image = Toolkit.getDefaultToolkit().getImage(
					getClass().getResource("/icon.gif")); //$NON-NLS-1$
			// create a action listener to listen for default action executed on the tray icon
			ActionListener actionListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setVisibility(Visibility.Visible);
				}
			};

			MouseListener mouseListener = new MouseListener() {
				public void mouseClicked(MouseEvent e) {
				}

				public void mouseEntered(MouseEvent e) {
				}

				public void mouseExited(MouseEvent e) {
				}

				public void mousePressed(MouseEvent e) {
					refreshState();
				}

				public void mouseReleased(MouseEvent e) {
				}
			};
			// create a popup menu
			PopupMenu popupTray = new PopupMenu();
			mniStart = new MenuItem(Messages.Main_ActionLabel_Start);
			mniStart.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					startActionPerformed();
				}
			});
			popupTray.add(mniStart);

			mniStop = new MenuItem(Messages.Main_ActionLabel_Stop);
			mniStop.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					stopActionPerformed();
				}
			});
			popupTray.add(mniStop);
			popupTray.addSeparator();

			mniExit = new MenuItem(Messages.Main_ActionLabel_Exit);
			mniExit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent evt) {
					exitActionPerformed();
				}
			});
			popupTray.add(mniExit);
			TrayIcon trayIcon = new TrayIcon(image, getTitle(), popupTray);
			//  set the TrayIcon properties
			trayIcon.addActionListener(actionListener);
			trayIcon.addMouseListener(mouseListener);
			// add the tray image
			try {
				tray.add(trayIcon);
			} catch (AWTException e) {
				System.err.println(e);
			}
		}
	}

	protected void refreshState() {
		boolean state=Application.getCurrentInstance().getIsPublished();
		setValue(StartEnabledProperty,!state);
		setValue(StopEnabledProperty,state);
		mniStop.setEnabled(state);
		mniStart.setEnabled(!mniStop.isEnabled());
	}

	protected void exitActionPerformed() {
		Application.getCurrentInstance().exit();
	}

	protected void stopActionPerformed() {
		Application.getCurrentInstance().endPublish();

	}

	private void startActionPerformed() {
		Application.getCurrentInstance().startPublish();
	}

	/**
	 * @param sender
	 * @param eventArgs
	 */
	public void Loaded(Object sender, RoutedEventArgs eventArgs) {
		initTray();
		Application.getCurrentInstance().registerObserver(this);
		Application.getCurrentInstance().initApp();
	}

	/**
	 * @param sender
	 * @param eventArgs
	 */
	public void guiStart_Click(Object sender, RoutedEventArgs eventArgs) {
		startActionPerformed();

	}

	/**
	 * @param sender
	 * @param eventArgs
	 */
	public void guiStop_Click(Object sender, RoutedEventArgs eventArgs) {
		stopActionPerformed();
	}

	/**
	 * @param sender
	 * @param eventArgs
	 */
	public void guiExit_Click(Object sender, RoutedEventArgs eventArgs) {
		exitActionPerformed();

	}
	
	/**
	 * @param sender
	 * @param eventArgs
	 */
	public void guiOptions_Click(Object sender, RoutedEventArgs eventArgs) {
		SettingsDialog dialog= new SettingsDialog();
		dialog.showDialog();
	}
	
	
	/**
	 * Generated Initialization.
	 */
	protected void initializeComponent() {
		mainWindow = (Window) findElement("mainWindow"); //$NON-NLS-1$
		btnStart = (Button) findElement("btnStart"); //$NON-NLS-1$
		stpControlPanel = (StackPanel) findElement("stpControlPanel"); //$NON-NLS-1$
		stpLog = (ListBox) findElement("stpLog"); //$NON-NLS-1$
		btnExit = (Button) findElement("btnExit"); //$NON-NLS-1$
		btnStop = (Button) findElement("btnStop"); //$NON-NLS-1$
	}

}